<template>
  <div class="discover-container">
    <!-- 页面头部 -->
    <header class="discover-header">
      <h1>发现</h1>
      <div class="header-actions">
        <button class="search-btn">
          <i class="fa fa-search"></i>
        </button>
        <button class="scan-btn">
          <i class="fa fa-qrcode"></i>
        </button>
      </div>
    </header>

    <!-- 推荐标签 -->
    <section class="hot-tags">
      <h2 class="section-title">热门标签</h2>
      <div class="tags-container">
        <div v-for="(tag, index) in hotTags" :key="index" class="tag-item" @click="handleTagClick(tag)">
          <div class="tag-icon" :style="{backgroundColor: tag.color}">
            <i :class="tag.icon"></i>
          </div>
          <span class="tag-name">{{ tag.name }}</span>
        </div>
      </div>
    </section>

    <!-- 精选内容 -->
    <section class="featured-content">
      <h2 class="section-title">精选推荐</h2>
      <div class="featured-grid">
        <div v-for="(item, index) in featuredItems" :key="index" class="featured-card">
          <img :src="item.image" :alt="item.title" class="featured-image">
          <div class="featured-info">
            <h3 class="featured-title">{{ item.title }}</h3>
            <div class="featured-stats">
              <span class="view-count"><i class="fa fa-eye"></i> {{ item.views }}</span>
              <span class="like-count"><i class="fa fa-heart"></i> {{ item.likes }}</span>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 热门话题 -->
    <section class="hot-topics">
      <h2 class="section-title">热门话题</h2>
      <div class="topics-list">
        <div v-for="(topic, index) in hotTopics" :key="index" class="topic-item">
          <div class="topic-rank">{{ index + 1 }}</div>
          <div class="topic-content">
            <h3 class="topic-title">{{ topic.title }}</h3>
            <p class="topic-desc">{{ topic.description }}</p>
          </div>
          <div class="topic-hot">{{ topic.hot }}</div>
        </div>
      </div>
    </section>

    <!-- 最新活动 -->
    <section class="latest-activities">
      <h2 class="section-title">最新活动</h2>
      <div class="activity-banner">
        <img src="@/assets/二次元美食节海报_3.jpg" alt="活动海报" class="activity-image">
        <div class="activity-overlay">
          <h3 class="activity-title">二次元美食节</h3>
          <p class="activity-time">2024.5.1 - 2024.5.15</p>
          <button class="join-btn">立即参与</button>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import { getUserAgeGroup, filterContentByAge, getAgeGroupPreferences, AGE_GROUPS } from '../services/ageRecommendationService.js'

export default {
  name: 'DiscoverView',
  data() {
    // 获取当前登录用户信息
    const getCurrentUser = () => {
      try {
        const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
        if (isLoggedIn) {
          const currentUsername = localStorage.getItem('currentUsername');
          if (currentUsername) {
            const registeredUsers = JSON.parse(localStorage.getItem('registeredUsers') || '[]');
            return registeredUsers.find(user => user.username === currentUsername);
          }
        }
      } catch (error) {
        console.error('获取用户信息失败:', error);
      }
      return null;
    };
    
    return {
      currentUser: getCurrentUser(),
      // 标签图标和颜色映射
      tagIcons: {
        '新番推荐': { icon: 'fa-star', color: '#FF6B6B' },
        '音乐现场': { icon: 'fa-music', color: '#4ECDC4' },
        '游戏攻略': { icon: 'fa-gamepad', color: '#45B7D1' },
        '科技前沿': { icon: 'fa-microchip', color: '#96CEB4' },
        '美食探店': { icon: 'fa-cutlery', color: '#FFEAA7' },
        '运动健身': { icon: 'fa-running', color: '#DDA0DD' },
        '宠物日常': { icon: 'fa-paw', color: '#F08080' },
        '电影解说': { icon: 'fa-film', color: '#9370DB' },
        '童话故事': { icon: 'fa-book', color: '#98D8C8' },
        '儿童动画': { icon: 'fa-dragon', color: '#F7DC6F' },
        '科学实验': { icon: 'fa-flask', color: '#7FB3D5' },
        '安全教育': { icon: 'fa-shield-alt', color: '#52BE80' },
        '电子音乐': { icon: 'fa-headphones', color: '#BB8FCE' },
        '舞蹈翻跳': { icon: 'fa-dance', color: '#F1948A' },
        '原神': { icon: 'fa-gem', color: '#F39C12' },
        '英雄联盟': { icon: 'fa-trophy', color: '#8E44AD' },
        'AI绘画': { icon: 'fa-paint-brush', color: '#E74C3C' },
        '生活vlog': { icon: 'fa-video', color: '#3498DB' },
        '学习方法': { icon: 'fa-book-open', color: '#2ECC71' },
        '旅行日记': { icon: 'fa-plane', color: '#E67E22' },
        '养老生活': { icon: 'fa-home', color: '#95A5A6' },
        '健康养生': { icon: 'fa-heartbeat', color: '#E74C3C' },
        '孙子孙女': { icon: 'fa-child', color: '#F1C40F' },
        '退休生活': { icon: 'fa-coffee', color: '#8B4513' },
      },
      // 精选内容数据
      allFeaturedItems: [
        {
          title: '2024年最值得期待的十大新番',
          image: '@/assets/动漫.png',
          views: '128.5万',
          likes: '5.2万',
        },
        {
          title: '科技数码展现场直击',
          image: '@/assets/数码科技展.png',
          views: '96.3万',
          likes: '3.8万',
        },
        {
          title: '周杰伦最新演唱会精彩瞬间',
          image: '@/assets/周杰伦.png',
          views: '156.8万',
          likes: '8.7万',
        },
        {
          title: '黑神话悟空最新实机演示分析',
          image: '@/assets/黑神话.jpeg',
          views: '234.1万',
          likes: '12.5万',
        },
      ],
      // 热门话题数据
      hotTopics: [
        {
          title: '#2024年游戏展',
          description: '全球各大游戏厂商的最新作品震撼发布',
          hot: '5.2亿',
        },
        {
          title: '#夏日动漫推荐',
          description: '这个夏天必看的10部热门动画',
          hot: '3.8亿',
        },
        {
          title: '#美食探店',
          description: '带你探索城市里的隐藏美食',
          hot: '2.9亿',
        },
        {
          title: '#科技新品发布',
          description: '最新科技产品抢先体验',
          hot: '2.5亿',
        },
        {
          title: '#生活小技巧',
          description: '提升生活品质的实用小窍门',
          hot: '1.8亿',
        },
      ],
    }
  },
  
  // 根据用户年龄获取热门标签
  computed: {
    hotTags() {
      // 获取标签列表（基于用户年龄）
      let tagNames;
      
      if (!this.currentUser || !this.currentUser.age) {
        // 未登录用户显示通用标签
        tagNames = ['新番推荐', '音乐现场', '游戏攻略', '科技前沿', '美食探店', '运动健身', '宠物日常', '电影解说'];
      } else {
        // 使用年龄推荐服务获取适合的标签
    const ageGroupKey = getUserAgeGroup(this.currentUser.age);
    const ageGroupName = AGE_GROUPS[ageGroupKey]?.name;
    const preferences = getAgeGroupPreferences(ageGroupName);
    // 返回适合的标签（这里使用分类作为标签）
    tagNames = preferences.categories;
      }
      
      // 添加图标和颜色
      return tagNames.map(name => ({
        name,
        ...(this.tagIcons[name] || { icon: 'fa-tag', color: '#95A5A6' })
      }));
    },
    
    // 根据用户年龄过滤的精选内容
    featuredItems() {
      if (!this.currentUser || !this.currentUser.age) {
        return this.allFeaturedItems;
      }
      
      // 使用年龄推荐服务过滤内容
      return filterContentByAge(this.allFeaturedItems, this.currentUser.age);
    }
  },
  methods: {
    handleTagClick(tag) {
      // 根据标签名称进行路由跳转
      if (tag.name === '新番推荐') {
        this.$router.push('/anime?tab=new')
      } else if (tag.name === '音乐现场') {
        this.$router.push('/music')
      } else if (tag.name === '游戏攻略') {
        this.$router.push('/game')
      } else if (tag.name === '科技前沿') {
        this.$router.push('/tech')
      } else if (tag.name === '美食探店') {
        this.$router.push('/food')
      } else if (tag.name === '运动健身') {
        this.$router.push('/sports')
      } else if (tag.name === '宠物日常') {
        this.$router.push('/animal')
      } else if (tag.name === '电影解说') {
        this.$router.push('/movie')
      }
    },
  },
}
</script>

<style scoped>
/* 发现页面容器 */
.discover-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  padding-bottom: 80px; /* 为底部导航留出空间 */
}

/* 页面头部 */
.discover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border-color);
}

.discover-header h1 {
  font-size: 28px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 15px;
}

.search-btn,
.scan-btn {
  width: 40px;
  height: 40px;
  border: none;
  background-color: var(--hover-bg-color);
  border-radius: 50%;
  cursor: pointer;
  font-size: 18px;
  color: var(--text-secondary);
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-btn:hover,
.scan-btn:hover {
  background-color: var(--primary-color);
  color: white;
  transform: scale(1.05);
}

/* 通用标题样式 */
.section-title {
  font-size: 22px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 30px 0 20px;
  position: relative;
  padding-left: 12px;
}

.section-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 20px;
  background-color: var(--primary-color);
  border-radius: 2px;
}

/* 热门标签 */
.tags-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 15px;
}

.tag-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 15px 10px;
  background-color: var(--card-bg);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid transparent;
}

.tag-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  border-color: var(--primary-color);
}

.tag-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  margin-bottom: 10px;
  transition: all 0.3s ease;
}

.tag-item:hover .tag-icon {
  transform: scale(1.1);
}

.tag-name {
  font-size: 14px;
  color: var(--text-primary);
  font-weight: 500;
}

/* 精选内容 */
.featured-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.featured-card {
  background-color: var(--card-bg);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid var(--border-color);
}

.featured-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.15);
}

.featured-image {
  width: 100%;
  height: 180px;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.featured-card:hover .featured-image {
  transform: scale(1.05);
}

.featured-info {
  padding: 15px;
}

.featured-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 10px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.featured-stats {
  display: flex;
  gap: 15px;
  font-size: 13px;
  color: var(--text-secondary);
}

.view-count,
.like-count {
  display: flex;
  align-items: center;
  gap: 5px;
}

/* 热门话题 */
.topics-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.topic-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--card-bg);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid var(--border-color);
}

.topic-item:hover {
  transform: translateX(10px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.topic-rank {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: var(--primary-color);
  color: white;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 14px;
}

.topic-item:nth-child(2) .topic-rank {
  background-color: #FFA500;
}

.topic-item:nth-child(3) .topic-rank {
  background-color: #CD7F32;
}

.topic-content {
  flex: 1;
  min-width: 0;
}

.topic-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 5px;
}

.topic-desc {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topic-hot {
  font-size: 14px;
  color: var(--primary-color);
  font-weight: 600;
  margin-left: 15px;
}

/* 最新活动 */
.activity-banner {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  margin-top: 10px;
  cursor: pointer;
}

.activity-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.activity-banner:hover .activity-image {
  transform: scale(1.03);
}

.activity-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px;
  background: linear-gradient(to top, rgba(0,0,0,0.8), rgba(0,0,0,0.2));
  color: white;
}

.activity-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 8px;
}

.activity-time {
  font-size: 14px;
  opacity: 0.9;
  margin: 0 0 12px;
}

.join-btn {
  padding: 8px 16px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.join-btn:hover {
  background-color: #E96385;
  transform: scale(1.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .discover-container {
    padding: 15px;
  }
  
  .discover-header h1 {
    font-size: 24px;
  }
  
  .section-title {
    font-size: 20px;
  }
  
  .tags-container {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 10px;
  }
  
  .tag-icon {
    width: 40px;
    height: 40px;
    font-size: 20px;
  }
  
  .featured-grid {
    grid-template-columns: 1fr;
  }
  
  .activity-image {
    height: 150px;
  }
}
</style>